
#include "iostream"
#include <bitset>
#include <vector>
#include "f_extern.h"

using namespace std;

int delete_note(int parent_num_block,int child_num_block)
{

	char name[10];
	int num_next = 0;
	int num_chld;
	int size,last_block_size;
	int cur_note;
	int cur_block,prev_block;
	int moved;
	header head;
	header chld_head;
	header parent_head;
	string path;
	string chld_path;
	FILE *ptr;
	FILE *fd;
		
	sprintf(name,"./.%d",parent_num_block);	
	path = root_dir; path.append(name);
	fd = fopen(path.data(),"rb");
	fread(&parent_head,sizeof(header),1,fd);
	fclose(fd);
	
	size = parent_head.size;
	cur_block = prev_block = num_next =  parent_head.data_block;
	do
	{
		sprintf(name,"./.%d",num_next);	
		path = root_dir; path.append(name);
		fd = fopen(path.data(),"rb");
		size -= (block_size-sizeof(int));
		fseek(fd,-sizeof(int),SEEK_END);
		fread(&num_next,sizeof(int),1,fd);
		fclose(fd);
		if(num_next!=0)
		{
			prev_block = cur_block; cur_block = num_next;
		}	
	}while(num_next!=0);
	
	size += (block_size-sizeof(int));
	sprintf(name,"./.%d",cur_block);	
	path = root_dir; path.append(name);
	fd = fopen(path.data(),"rb+");
	
	if(size == sizeof(int))
	{
		fseek(fd,0,SEEK_SET);
		fread(&moved,sizeof(int),1,fd);
		fclose(fd);
		sprintf(name,"./.%d",prev_block);	
		path = root_dir; path.append(name);
		fd = fopen(path.data(),"rb+");
		fseek(fd,-sizeof(int),SEEK_END);
		cur_block = 0;
		fwrite(&cur_block,sizeof(int),1,fd);
		fclose(fd);
		parent_head.size_in_block--;
	}
	else
	{
		fseek(fd,(size-sizeof(int)),SEEK_SET);
		fread(&moved,sizeof(int),1,fd);
		fclose(fd);
	}
	if(moved!=child_num_block)
	{
		size = parent_head.size;
		num_next =  parent_head.data_block;
		do
		{
			sprintf(name,"./.%d",num_next);	
			path = root_dir; path.append(name);
			fd = fopen(path.data(),"rb+");
			
			for(int i=0;i<parent_head.size&&block_size-(sizeof(int));i++)
			{
				fread(&cur_note,1,sizeof(int),fd);
				if(cur_note == child_num_block)
				{
					num_next=0;
					break;
				}
			}
			if(cur_note != child_num_block)
			
			{
				size -= (block_size-sizeof(int));
				fseek(fd,-sizeof(int),SEEK_END);
				fread(&num_next,sizeof(int),1,fd);
				fclose(fd);
		
			}		
		}while(num_next!=0);
		
		if(cur_note != child_num_block)
		{
			fclose(fd);
			printf("[rm]: \n");
			return 1;
		}
		else
		{
			fseek(fd,-sizeof(int),SEEK_CUR);
			fwrite(&moved,sizeof(int),1,fd);
			fclose(fd);
		}
	}
	
	sprintf(name,"./.%d",parent_num_block);	
	path = root_dir; path.append(name);
	fd = fopen(path.data(),"rb+");
	parent_head.size-=sizeof(int);
	set_time(&parent_head);
	fseek(fd,0,SEEK_SET);
	fwrite(&parent_head,sizeof(header),1,fd);
	fclose(fd);
	return 0;

}

int rm_dir()
{
	char name[10];
	int num_next = 0;
	int num_chld;
	int cur_block_size,last_block_size;
	int tmp_size;
	header head;
	header chld_head;
	header parent_head;
	string path;
	string chld_path;
	FILE *ptr;
	FILE *fd;
	int base_block, base_block_prev,parent_block;

	parent_block = base_block = base_block_prev =1;
	for(vector<string>::iterator i=dir_set.begin();i!=dir_set.end();i++)
	{
		base_block_prev = base_block;
		sprintf(name,"./.%d",base_block);	
		path = root_dir; path.append(name);
		fd = fopen(path.data(),"rb");
		fread(&head,sizeof(header),1,fd);
		fclose(fd);
		
		num_next = head.data_block;	
		do
		{
			sprintf(name,"./.%d",num_next);
			path = root_dir; path.append(name);
			fd = fopen(path.data(),"rb");
			for(int j=0;j<head.size && j<block_size-(sizeof(int)); j+=sizeof(int))
			{
				
				fread(&num_chld,sizeof(int),1,fd);
				sprintf(name,"./.%d",num_chld);
				chld_path = root_dir; chld_path.append(name);
				ptr = fopen(chld_path.data(),"rb");
				fread(&chld_head,sizeof(header),1,ptr);
				fclose(ptr);
				if(strcmp((*i).data(),chld_head.name)==0)
				{
					if(i!=dir_set.end()-1)
					{
						if(chld_head.type==0)
						{
							parent_block = base_block_prev;
							base_block = num_chld;
							break;
						}
					}
					else
					{
						parent_block = base_block_prev;
						base_block = num_chld;
						break;
					}
					
				}
				
			}
			if(base_block_prev == base_block){
			
				head.size -= (block_size-sizeof(int));
				fseek(fd,-sizeof(int),SEEK_END);
				fread(&num_next,sizeof(int),1,fd);
				fclose(fd);
			}
			else
			{
				fclose(fd);
				break;
				
			}
		}while(num_next!=0);
		
		if(base_block_prev == base_block)
		{
#ifdef PRINT_MSG
			printf("%s:Directory/File not exist \n");
#endif	
			return 3;
		}
	}

	if(delete_note(parent_block,base_block)!=0)
		return 1;
	else	
		return 0;

}

int main(int argc, char* argv[])
{	
	char* _f_ = "[rm]";
	int next_dir_num = 0;
	int i; 
	FILE* config = NULL; 
	FILE* block = NULL;
	string file_name;
	
	header dir;
	string dir_path;
	if (argc != 3){
#ifdef PRINT_MSG
		printf("%s: Use rm root path \n",_f_);
#endif
	return 2;
	}
	
		root_dir = argv[1];
		make_build_map(1);
		dir_path = argv[2];
		parse_path(dir_path);

		return rm_dir();	
}


